package com.kgc.skProvider.service.serviceimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.SalesAdvance;
import com.kgc.collection.pojo.SalesAdvanceDetail;
import com.kgc.skProvider.dao.SalesAdvanceMapper;
import com.kgc.skProvider.service.SalesAdvanceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SalesAdvanceServiceImpl implements SalesAdvanceService {

    private Logger logger= LoggerFactory.getLogger(SalesAdvanceServiceImpl.class);

    @Autowired
    private SalesAdvanceMapper salesAdvanceMapper;

    @Override
    public ResponseMsg selectSalesAdvanceByPage(Map<String, Object> params) {
        logger.info("SalesAdvanceServiceImpl selectSalesAdvanceByPage params is{}",params.toString() );
        ResponseMsg responseMsg=new ResponseMsg();
        PageInfo<SalesAdvance> pageInfo= new PageInfo<>();
        pageInfo.setPageNo(params.get("pageNo")==null?1:Integer.parseInt(params.get("pageNo").toString()));
        pageInfo.setPageSize(params.get("pageSize")==null?10:Integer.parseInt(params.get("pageSize").toString()));
        Page page = PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<SalesAdvance> list=salesAdvanceMapper.selectSalesAdvanceByPage(params);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("SalesAdvanceServiceImpl selectSalesAdvanceByPage pageInfo is{}",pageInfo.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg insertSalesAdvance(Map<String, Object> params) {
        logger.info("SalesAdvanceServiceImpl insertSalesAdvance params is{}",params.toString() );
        ResponseMsg responseMsg=new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();
        SalesAdvance salesAdvance=mapper.convertValue(params.get("salesAdvance"), SalesAdvance.class);
        List<?> rawDetails = (List<?>) params.get("details");
        List<SalesAdvanceDetail> details=mapper.convertValue(rawDetails,new TypeReference<List<SalesAdvanceDetail>>(){});
        int result=salesAdvanceMapper.insertSalesAdvance(salesAdvance);
        try {
            for(SalesAdvanceDetail detail:details){
                salesAdvanceMapper.insertSalesAdvanceDetail(detail);
            }
            responseMsg.setCode("200");
        }catch (Exception e){
            e.printStackTrace();
            responseMsg.setCode("201");
            return responseMsg;
        }
        if(result>0){
            responseMsg.setCode("200");
        }else{
            responseMsg.setCode("201");
        }
        logger.debug("SalesAdvanceServiceImpl insertSalesAdvance is success result is{}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg updateSalesAdvance(Map<String, Object> params) {
        logger.info("SalesAdvanceServiceImpl updateSalesAdvance params is{}",params.toString() );
        deleteSalesAdvance(params);
        ResponseMsg responseMsg=insertSalesAdvance(params);
        logger.debug("SalesAdvanceServiceImpl updateSalesAdvance is success result is{}",responseMsg.toString());
        return responseMsg;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseMsg deleteSalesAdvance(Map<String, Object> params) {
        logger.info("SalesAdvanceServiceImpl deleteSalesAdvance params is{}",params.toString() );
        ResponseMsg responseMsg=new ResponseMsg();
        int result1= salesAdvanceMapper.deleteSalesAdvance(params.get("docCode").toString());
        int result2= salesAdvanceMapper.deleteSalesAdvanceDetail(params.get("docCode").toString());
        if(result1>0 && result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesAdvanceServiceImpl deleteSalesAdvance is success result is{}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesAdvanceDetail(Map<String, Object> params) {
        logger.info("SalesAdvanceServiceImpl selectSalesAdvanceDetail params is{}",params.toString() );
        ResponseMsg responseMsg=new ResponseMsg();
        List<SalesAdvanceDetail> details=salesAdvanceMapper.selectSalesAdvanceDetail(params);
        responseMsg.setData(details);
        logger.debug("SalesAdvanceServiceImpl selectSalesAdvanceDetail is success result is{}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg checkSalesAdvance(Map<String, Object> params) {
        logger.info("SalesAdvanceServiceImpl checkSalesAdvance params is{}",params.toString() );
        ResponseMsg responseMsg=new ResponseMsg();
        int result=salesAdvanceMapper.checkSalesAdvance(params);
        if(result>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.debug("SalesAdvanceServiceImpl checkSalesAdvance is success result is{}",responseMsg.toString());
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesAdvanceByCode(Map<String, Object> params) {
        logger.info("SalesAdvanceServiceImpl selectSalesAdvanceByCode params is{}",params.toString() );
        ResponseMsg responseMsg=new ResponseMsg();
        SalesAdvance salesAdvance=salesAdvanceMapper.selectSalesAdvanceByCode(params);
        responseMsg.setData(salesAdvance);
        List<SalesAdvanceDetail> details=salesAdvanceMapper.selectSalesAdvanceDetail(params);
        salesAdvance.setDetails(details);
        responseMsg.setCode("200");
        responseMsg.setData(salesAdvance);
        logger.debug("SalesAdvanceServiceImpl selectSalesAdvanceByCode return is success result is{} ",responseMsg.toString());
        return responseMsg;
    }
}
